package com.easy.component.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Bundle;

import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.easy.component.R;

/**
 * 描述: {dialog loadin}
 * 作者: jack-song
 * 创建日期: 2017/11/7 10:04
 * 修改日期: 2017/11/7
 * 版本: {版本}
 */
public class EasyLoadingDialog extends Dialog {
    /**
     * 提示
     */
    private TextView tvLoadingMsg;
    /**
     * 提示
     */
    private CharSequence message;
    /**
     * 是否可以取消
     */
    private boolean isCancelable;
    /**
     * 是否关闭activtiy
     */
    private boolean isCloseActivity;
    /**
     * 监听dialog返回
     */
    private OnBackPressedListener onBackPressedListener;

    public EasyLoadingDialog(@NonNull Context context) {
        super(context);
    }

    public EasyLoadingDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.easy_dialog_loading);
        tvLoadingMsg = findViewById(R.id.tv_loading_msg);
        if (TextUtils.isEmpty(message)) {
            tvLoadingMsg.setVisibility(View.GONE);
            tvLoadingMsg.setText("");
        } else {
            tvLoadingMsg.setVisibility(View.VISIBLE);
            tvLoadingMsg.setText(message);
        }
        this.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == 4 && isCloseActivity && null != onBackPressedListener) {
                    onBackPressedListener.onBackPressed();
                    return true;
                } else {
                    return false;
                }
            }
        });
        setCancelable(isCancelable);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        window.getAttributes().width = (int) (windowSize.x * 0.8f);
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);

    }

    public void setCloseActivity(boolean closeActivity) {
        isCloseActivity = closeActivity;
    }

    public void setCancelabled(boolean isCancelable) {
        this.isCancelable = isCancelable;
    }

    public void setMessage(CharSequence message) {
        this.message = message;
    }

    public void setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
        this.onBackPressedListener = onBackPressedListener;
    }

    public interface OnBackPressedListener {
        void onBackPressed();
    }


    public static class Builder {
        /**
         * 上下文
         */
        private Context context;
        /**
         * 提示
         */
        private CharSequence message;
        /**
         * 是否可以取消
         */
        private boolean isCancelable;
        /**
         * 是否关闭activtiy
         */
        private boolean isCloseActivity;
        /**
         * 监听dialog返回
         */
        private OnBackPressedListener onBackPressedListener;

        public Builder() {
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setCloseActivity(boolean closeActivity) {
            isCloseActivity = closeActivity;
            return this;
        }

        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        public Builder setMessage(CharSequence message) {
            this.message = message;
            return this;
        }

        public Builder setOnBackPressedListener(OnBackPressedListener onBackPressedListener) {
            this.onBackPressedListener = onBackPressedListener;
            return this;
        }

        public EasyLoadingDialog builder() {
            EasyLoadingDialog loadingDialog = new EasyLoadingDialog(context, R.style.loading_dialog);
            loadingDialog.setCancelabled(isCancelable);
            loadingDialog.setCloseActivity(isCloseActivity);
            loadingDialog.setMessage(message);
            loadingDialog.setOnBackPressedListener(onBackPressedListener);
            return loadingDialog;
        }
    }
}
